package com.aaa.tars.service.impl;

import com.aaa.tars.dao.ScenicAuditDao;
import com.aaa.tars.dao.ScenicDao;
import com.aaa.tars.entity.Scenic;
import com.aaa.tars.entity.ScenicAudit;
import com.aaa.tars.entity.User;
import com.aaa.tars.service.ScenicAuditService;
import com.aaa.tars.util.CustomException;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @FileName: ScenicAuditServiceImpl
 * @Description:
 * @Author: xm
 * @CreateTime: 2025/9/24 15:18
 * @Version: 1.0.0
 */
@Service
public class ScenicAuditServiceImpl implements ScenicAuditService {
    @Resource
    private ScenicAuditDao scenicAuditDao;
    @Resource
    private ScenicDao scenicDao;
    @Resource
    private HttpSession session;

    @Override
    public ScenicAudit queryById(Integer auditId) {
        if(auditId==null||auditId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        return scenicAuditDao.queryById(auditId);
    }
    @Override
    public ScenicAudit queryByScenicId(Integer scenicId) {
        if(scenicId==null||scenicId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        return scenicAuditDao.queryByScenicId(scenicId);
    }

    @Override
    public PageInfo queryByPage(Page<ScenicAudit> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();

        if(pageNo==null||pageNo==0||pageSize==null||pageSize==0){
            throw  new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        PageHelper.startPage(pageNo,pageSize);
        ScenicAudit scenicAudit = page.getData();
        List<ScenicAudit> scenicAuditList = scenicAuditDao.queryAllByLimit(scenicAudit);
        return new PageInfo(scenicAuditList);
    }

    @Override
    public int insert(ScenicAudit scenicAudit) {
        return scenicAuditDao.insert(scenicAudit);
    }

    @Override
    public int update(ScenicAudit scenicAudit) {
        //动态获取创建人
        //拿到登录session
        User userInfo = (User) session.getAttribute("userInfo");
        scenicAudit.setAuditPeople(userInfo.getUserName());
        // 设置更新时间
        scenicAudit.setAuditTime(LocalDateTime.now());
        //如果状态为通过，清空拒绝理由
        if(scenicAudit.getAuditStatus().equals("1")){
            scenicAudit.setRejectReason("");
        }
        return scenicAuditDao.update(scenicAudit);
    }

    @Override
    public int updateByScenicId(ScenicAudit scenicAudit) {
        return scenicAuditDao.updateByScenicId(scenicAudit);
    }



    @Override
    public int deleteById(Integer auditId) {
        if(auditId==null||auditId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        //如果待审核状态为待审核，则不能删除
        ScenicAudit scenicAudit = scenicAuditDao.queryById(auditId);
        if(scenicAudit.getAuditStatus().equals("0")){
            //抛出报错信息
            throw new CustomException(1020,"未审核，不能删除");
        }
        //返回设置删除状态为1
        scenicAudit.setDelFlag("1");
        return scenicAuditDao.update(scenicAudit);
    }

}
